package com.youku.component.result;

import java.util.Map;

import org.junit.Assert;

/**
 * http请求的响应结果
 * 
 * @author loda
 *
 */
public class HttpExecuteResult implements ExecuteResult {

	private String body;

	private String status;

	private ResponseBodyHandler handler;

	private Map<String, String> cookies;

	public HttpExecuteResult(ResponseBodyHandler handler) {
		this.handler = handler;
	}

	public HttpExecuteResult() {
		// 默认采用json方式处理
		this(new JsonResponseBodyHandler());
	}

	public void addResponseCookie(String key, String value) {
		cookies.put(key, value);
	}

	public String getCookieByKey(String key) {
		return cookies.get(key);
	}

	public String getResponseBody() {
		return body;
	}

	public void setResponseBody(String responseBody) {
		this.body = responseBody;
	}

	public String getResponseStatus() {
		return status;
	}

	public void setResponseStatus(String responseStatus) {
		this.status = responseStatus;
	}

	public Map<String, String> getResponseCookies() {
		return cookies;
	}

	public void setResponseCookies(Map<String, String> responseCookies) {
		this.cookies = responseCookies;
	}

	public String getResponseBodyByField(String field) {
		Assert.assertNotNull("获取响应体信息不能为空", body);
		Assert.assertNotNull("获取响应体中的指定路径元素的值不能为空", field);
		handler.init(body);
		return handler.handle(field);
	}

}
